//
//  AIInterstedView.m
//  Mia
//
//  Created by Vill on 2023/12/29.
//

#import "AIInterstedView.h"
#import "CZHTagsView.h"
#import "AIInterstedDataModel.h"

@interface AIInterstedView ()<CZHTagsViewDelegate, CZHTagsViewDataSource>

@property (nonatomic, strong) UIButton *backBtn;     //返回
@property (nonatomic, strong) UIButton *skipBtn;     //跳过
@property (nonatomic, strong) UILabel *interTitle;   //标题
@property (nonatomic, strong) UIView *botoomView;    //底部
@property (nonatomic, strong) UIVisualEffectView *blurView; //模糊

@property (nonatomic, strong) CZHTagsView *tagsView; // 标签view
@property (nonatomic, strong) UIButton *doneBtn;

@property (nonatomic, strong) NSMutableArray *tagsArray;  //标签数组
@property (nonatomic, strong) NSArray *interestTagIds;  //选择的兴趣标签按钮

@property (nonatomic, strong) NSMutableArray *getTagsArr; // 请求得到的数组

@end

@implementation AIInterstedView

- (NSMutableArray *)getTagsArr{
    if(!_getTagsArr){
        _getTagsArr = [NSMutableArray array];
    }
    return _getTagsArr;
}

// 选中的标签
- (NSArray *)interestTagIds{
    if(!_interestTagIds){
        _interestTagIds = [NSArray array];
    }
    return _interestTagIds;
}

// 标签数组
- (NSMutableArray *)tagsArray{
    if(!_tagsArray){
        _tagsArray = [NSMutableArray array];
    }
    return _tagsArray;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if(self == [super initWithFrame:frame]){
        [self getInterstedData];
    }
    return self;
}

// 获取兴趣标签数组
- (void)getInterstedData{
    [SVProgressHUD show];
    [AIManager requestMethod:@"GET" url:api_getAllTags parameters:@{} successBlock:^(id  _Nullable result) {
        [self.getTagsArr removeAllObjects];
        [self.tagsArray removeAllObjects];
        NSArray *tempArr = result;
        if (tempArr.count > 0){
            for (int i = 0; i<tempArr.count; i++) {
                AIInterstedDataModel *datamodel = [AIInterstedDataModel mj_objectWithKeyValues:tempArr[i]];
                [self.getTagsArr addObject:datamodel];
                [self.tagsArray addObject:datamodel.interestTagName];
            }
        }
        [SVProgressHUD dismiss];
        [self setUi];  //设置UI
    } failedBlock:^(NSError * _Nullable error) {
        [SVProgressHUD dismiss];
    } showHUD:NO];
}

-(void)setUi{
    [self addSubview:self.backBtn];
    [self addSubview:self.interTitle];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(PTW(34));
        make.leading.mas_equalTo(self).mas_offset(PTW(13));
        make.top.mas_equalTo(self).mas_offset(KStatusBarHeight + PTW(5));
    }];
    [self.interTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.backBtn.mas_bottom).mas_offset(PTW(25));
    }];
    [self addSubview:self.skipBtn];
    [self.skipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(PTW(50), PTW(40)));
        make.centerY.mas_equalTo(self.backBtn);
        make.trailing.mas_equalTo(self);
    }];
    
    [self addSubview:self.botoomView];
    [self.botoomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).mas_offset(PTW(12.5));
        make.trailing.mas_equalTo(self).mas_offset(-PTW(12.5));
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(PTW(300)+KBottomSafeHeight);
    }];
        
    [self.botoomView addSubview:self.blurView];
    [self.blurView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.botoomView);
    }];
    
    CZHTagsView *tag = [[CZHTagsView alloc]initWithFrame:CGRectMake(PTW(0),PTW(35), KScreenWidth - PTW(25), PTW(180)) style:CZHTagsViewStyleDefault selectMode:CZHTagsViewSelectModeMulti displayMode:CZHTagsViewDisplayModePaddingFirst];
    tag.dataSource = self;
    tag.delegate = self;
    [self.botoomView addSubview:tag];
    self.tagsView = tag;
    [self.tagsView reloadData];
    
    [self.botoomView addSubview:self.doneBtn];
    [self.doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.botoomView).mas_offset(PTW(18));
        make.trailing.mas_equalTo(self.botoomView).mas_offset(-PTW(18));
        make.height.mas_equalTo(PTW(55));
        make.top.mas_equalTo(tag.mas_bottom).mas_offset(PTW(10));
    }];
}

- (UIButton *)backBtn{
    if(!_backBtn){
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:KImaged(@"ai_base_back") forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (UIButton *)skipBtn{
    if(!_skipBtn){
        _skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_skipBtn setTitle:@"Skip" forState:UIControlStateNormal];
        _skipBtn.titleLabel.font = [UIFont fontWithWeight:MTBold size:PTW(15)];
        [_skipBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_skipBtn addTarget:self action:@selector(skipAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _skipBtn;
}

-(UILabel *)interTitle{
    if(!_interTitle){
        _interTitle = [UILabel new];
        _interTitle.text = @"What topics are you \ninterested in?";
        _interTitle.numberOfLines = 0;
        _interTitle.textAlignment = NSTextAlignmentCenter;
        _interTitle.textColor = UIColor.whiteColor;
        _interTitle.font = [UIFont fontWithWeight:MTBold size:PTW(27)];
    }
    return _interTitle;
}

- (UIView *)botoomView{
    if(!_botoomView){
        _botoomView = [UIView new];
        _botoomView.backgroundColor = UIColor.clearColor;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self->_botoomView clipsCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:PTW(20)];
        });
    }
    return _botoomView;
}

- (UIVisualEffectView *)blurView{
    if(!_blurView){
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        _blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        _blurView.alpha = 0.6;
        _blurView.backgroundColor = [UIColor colorHex:@"#FFFFFF" alpha:0.2];
    }
    return _blurView;
}

- (UIButton *)doneBtn{
    if(!_doneBtn){
        _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_doneBtn setTitle:@"Done" forState:UIControlStateNormal];
        [_doneBtn setTitleColor:[UIColor colorHex:@"#5C5C5C"] forState:UIControlStateNormal];
        _doneBtn.titleLabel.font = [UIFont fontWithWeight:MTBold size:PTW(15)];
        _doneBtn.backgroundColor = [UIColor colorHex:@"#EFEFEF" alpha:0.5];
        _doneBtn.layer.cornerRadius = PTW(55)/2;
        _doneBtn.layer.shadowColor = [UIColor colorHex:@"#FFFFFF" alpha:0.68].CGColor;
        _doneBtn.layer.shadowOffset = CGSizeMake(0,0);
        _doneBtn.layer.shadowOpacity = 1;
        _doneBtn.layer.shadowRadius = 8.5;
        [_doneBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
        _doneBtn.enabled = NO;
    }
    return _doneBtn;
}

//- (void)drawRect:(CGRect)rect{
//    [super drawRect:rect];
//    [self.botoomView clipsCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:PTW(20)];
//}

#pragma mark --------------------- CZHTagsView 数据源 代理 --------------------------
-(NSArray *)czh_tagsArrayInTagsView:(CZHTagsView *)tagsView{
    return self.tagsArray;
}
- (UIFont *)czh_fontForItemInTagsView:(CZHTagsView *)tagsView {
    return [UIFont fontWithWeight:MTBold size:PTW(17)];
}
//内边距 默认(0,0,0,0)
- (UIEdgeInsets)czh_insetForTagsView:(CZHTagsView *)tagsView{
    return UIEdgeInsetsMake(PTW(0), PTW(12), PTW(0), PTW(12));
}

//每一行之前的距离 默认10
- (CGFloat)czh_marginHeightForRowInTagsView:(CZHTagsView *)tagsView{
    return PTW(10);
}
- (CGFloat)czh_heightForItemInTagsView:(CZHTagsView *)tagsView{
    return PTW(50);
}
//边框颜色 默认黑色
- (UIColor *)czh_borderColorForItemInTagsView:(CZHTagsView *)tagsView{
    return UIColor.clearColor;
}
//选中边框颜色 默认黑色
- (UIColor *)czh_selectBorderColorForItemInTagsView:(CZHTagsView *)tagsView{
    return UIColor.whiteColor;
}
- (UIColor *)czh_textColorForItemInTagsView:(CZHTagsView *)tagsView{
    return UIColor.whiteColor;
}
- (UIColor *)czh_selectTextColorForItemInTagsView:(CZHTagsView *)tagsView{
    return UIColor.whiteColor;
}
//背景颜色 默认透明
- (UIColor *)czh_backgroundColorForItemInTagsView:(CZHTagsView *)tagsView{
    return [UIColor colorHex:@"#FFFFFF" alpha:0.3];
}
//选中状态背景颜色 默认透明
-(UIColor *)czh_selectBackgroundColorForItemInTagsView:(CZHTagsView *)tagsView{
    return [UIColor colorHex:@"#FFFFFF" alpha:0.3];
}
- (CGFloat)czh_cornerRadiusForItemInTagsView:(CZHTagsView *)tagsView{
    return PTW(25);
}
- (CGFloat)czh_borderWidthForItemInTagsView:(CZHTagsView *)tagsView{
    return PTW(2);
}
- (CGFloat)czh_paddingWidthForItemInTagsView:(CZHTagsView *)tagsView{
    return PTW(15);
}
//选中的标签
- (void)czh_tagsView:(CZHTagsView *)tagsView didSelectItemWithSelectTagsArray:(NSArray *)selectTagsArray{
    if (selectTagsArray.count > 0){
        self.doneBtn.enabled = YES;
        self.doneBtn.backgroundColor = [UIColor colorHex:@"#FFFFFF"];
    }else{
        self.doneBtn.enabled = NO;
        self.doneBtn.backgroundColor = [UIColor colorHex:@"#EFEFEF" alpha:0.5];
    }
    self.interestTagIds = selectTagsArray;
    NSLog(@"selectTagsArray: --  %@",self.interestTagIds);
}

#pragma mark ------------------ 按钮点击 ----------------
// 返回
-(void)backAction{
    if(self.backActionBlock){
        self.backActionBlock();
    }
}

// 跳过
-(void)skipAction{
    if (self.skipActionBlock){
        self.skipActionBlock();
    }
}

// 确认点击
-(void)doneAction{
    NSMutableArray *temArr = [NSMutableArray array];
    if (self.getTagsArr.count > 0){
        for (int i=0; i<self.getTagsArr.count; i++) {
            AIInterstedDataModel *datamodel = self.getTagsArr[i];
            for (int j = 0; j<self.interestTagIds.count; j++) {
                NSString *interestTagName = self.interestTagIds[j];
                if ([interestTagName isEqualToString:datamodel.interestTagName]){
                    [temArr addObject:datamodel.interestTagId];
                }
            }
        }
    }
    if (self.doneActionBlock){
        self.doneActionBlock(temArr);
    }
}

@end
